﻿
		Ext.onReady(function(){
				
				Ext.QuickTips.init();
				
				var currentClickOrgId;
				
				var currentClickOrgName;
				
				var tree = getOrgTree();
				
				var processMask = new Ext.LoadMask(Ext.getBody());
				
				//create the org detail window;
				var win = getWindow();
				//alert(win.findByType('form')[0].buttons[0]); 
				
			
				var addOrgAction = new Ext.Action({
						text : '新增下属部门',
						iconCls : 'all',
						handler : function() {
							if(currentClickOrgId==null||currentClickOrgId=="undefined"||currentClickOrgId==""){
								Ext.Msg.alert("提示","请先选择上级部门!");
							}else{
								win.setTitle('新增下属部门');
		   					//win.findByType('form')[0].getForm().reset();
								win.findById('org_id').setValue(null);
								win.findById('org_code').setValue('');
								win.findById('org_name').setValue('');
								win.findById('org_memo').setValue('');
								win.findById('parentorg_id').setValue(currentClickOrgId);
								win.findById('parent_name').setText('上级组织机构名称: ' + currentClickOrgName); 
								win.findById('operation').setValue(1); 
								win.show();
								}
						}
				});
				
				var editOrgAction = new Ext.Action({
						text : '修改部门信息',
						iconCls : 'all',
						handler : function() {
							if(currentClickOrgId==null||currentClickOrgId=="undefined"||currentClickOrgId==""){
								Ext.Msg.alert("提示","请先选择要修改的部门!");
							}else{
								processMask.msg = "数据加载中，请稍候...";
								processMask.show();
								Ext.Ajax.request({
									url:"getOrgInfo.action",
									params: { id: currentClickOrgId},
									method:"post",
									success: function(response,options){
											processMask.hide();
											win.setTitle('修改部门信息');
											//win.findByType('form')[0].getForm().reset();
											
											var responseTextValue = Ext.util.JSON.decode(response.responseText);
											
											win.findById('org_code').setValue(Ext.util.JSON.decode(responseTextValue.jsonString)[0].org_code);
											
											if(Ext.util.JSON.decode(responseTextValue.jsonString)[0].org_memo != "null"){
												win.findById('org_memo').setValue(Ext.util.JSON.decode(responseTextValue.jsonString)[0].org_memo);
											}else{
												win.findById('org_memo').setValue('');
											}
											
											if (tree.getRootNode().id != currentClickOrgId){
												win.findById('parent_name').setText('上级组织机构名称: ' + tree.getNodeById(currentClickOrgId).parentNode.text);
												win.findById('parentorg_id').setValue(tree.getNodeById(currentClickOrgId).parentNode.id); 
											}else{
												win.findById('parent_name').setText('');
												win.findById('parentorg_id').setValue(null);
												}
											win.findById('operation').setValue(2);
											win.findById('org_id').setValue(currentClickOrgId);
											win.findById('org_name').setValue(Ext.util.JSON.decode(responseTextValue.jsonString)[0].text);
											win.show();
										},
									failure : function(response,options){
											processMask.hide();
										  Ext.Msg.alert('提示','获取部门信息失败！');
										}		
								});
							}
						}
				});
				
				var delOrgAction = new Ext.Action({
						text : '删除选中部门',
						iconCls : 'all',
						handler : function() {
							if(currentClickOrgId==null||currentClickOrgId=="undefined"||currentClickOrgId==""){
								Ext.Msg.alert("提示","请先选择要删除的部门!");
							}else{
								Ext.MessageBox.show({
									title:'提示',
									msg:'确定要删除 ' + currentClickOrgName + ' 吗?',
									buttons:Ext.MessageBox.OKCANCEL,
									fn:function(btn) {
										if (btn == "ok"){
											processMask.msg = "处理中，请稍候...";
											processMask.show();
											Ext.Ajax.request({
												url:"modifyOrg.action",
												params:{org_id:currentClickOrgId,operation:3},
												method:"post",
												success: function(response,options){
													processMask.hide(); 
													var responseTextValue = Ext.util.JSON.decode(response.responseText);
													switch(Ext.util.JSON.decode(responseTextValue.jsonString).errcode){
														case -1:
															Ext.Msg.alert("警告","当前部门拥有有效的下级部门，无法删除！");
															break;
														case -2:
															Ext.Msg.alert("警告","当前部门拥有有效的雇员，无法删除！");
															break;
														default:
															tree.getNodeById(currentClickOrgId).remove();
															currentClickOrgId = null;
															currentClickOrgName = null;
															break;
														}
														
													}	,
												failure:function(response,options){
														processMask.hide();
														var responseTextValue = Ext.util.JSON.decode(response.responseText);
														Ext.Msg.alert("警告","错误编号: "+Ext.util.JSON.decode(responseTextValue.jsonString).errcode);
													}
											});
										}
										if (btn == "cancel"){
											return;
										}
									}
								});
							}
						}
				});

				tree.add(new Ext.Toolbar({items:[addOrgAction,'-', editOrgAction,'-', delOrgAction,'-']}));
				
				win.findByType('form')[0].on('beforeaction',function(form, action) {
							processMask.msg = "处理中，请稍候...";
							processMask.show();
					});
				
				win.findByType('form')[0].on('actioncomplete',function(form, action) {
								
								processMask.hide();
					    	switch(form.findField('operation').value){
					    		
					    		case 1:
					    		var result = Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString);
					    		if(form.findField('parentorg_id').value == tree.getRootNode().id ){
					    				tree.getRootNode().appendChild(new Ext.tree.AsyncTreeNode({
					    				id:Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString).id,
						      		text:Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString).text,
						      		leaf:Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString).leaf})
						      			);
						      	}else{
						      		tree.getNodeById(form.findField('parentorg_id').value).leaf=false;
					    				tree.getNodeById(form.findField('parentorg_id').value).appendChild(new Ext.tree.AsyncTreeNode({
					    				id:Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString).id,
						      		text:Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString).text,
						      		leaf:Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString).leaf})
						      			);
						      			tree.getNodeById(form.findField('parentorg_id').value).expand();
						      		}
					    			break;
					    		case 2:
					    		var result = Ext.util.JSON.decode(Ext.util.JSON.decode(action.response.responseText).jsonString);
					    			if(form.findField('org_id').value == tree.getRootNode().id ){
					    				tree.getRootNode().setText(result.text);
					    			}else{
					    				tree.getNodeById(form.findField('org_id').value).setText(result.text);
					    				}
					    			break;
					    		default:
					    			break
					    		}
				});
				
				win.findByType('form')[0].on('actionfailed ',function(form, action) {
						processMask.hide();
						Ext.Msg.alert('警告','数据提交失败');
					});
  			
		    // 定义树移动前事件
		    tree.on('beforenodedrop', function(dropEvent){
   				if ((dropEvent.point == 'append') && (dropEvent.dropNode.parentNode.id != dropEvent.target.id)) {
   					Ext.MessageBox.show({
									title:'提示',
									msg:'是否将 ' + dropEvent.dropNode.text + ' 移动到 ' + dropEvent.target.text + ' 下面？',
									buttons:Ext.MessageBox.OKCANCEL,
									fn:function(btn) {
										if (btn == "ok"){
											Ext.Ajax.request({
													url:"modifyOrg.action",
													params:{org_id:dropEvent.dropNode.id,operation:2,parentorg_id:dropEvent.target.id},
													method:"post",
													callback : function(options, success, response ){
														if(success){
															if(dropEvent.target.isLoaded()){
																dropEvent.target.appendChild(dropEvent.dropNode);
															}
															else{
																dropEvent.dropNode.remove();
																}
															dropEvent.target.expand();
														}
														else{
															Ext.Msg.Alert('警告','移动 '+ dropEvent.dropNode.text +" 失败！");
														}
													}		
											});
										}
									}
								});	

   				}else if(((dropEvent.point == 'above')||(dropEvent.point == 'below'))&&( dropEvent.dropNode.parentNode.id != dropEvent.target.parentNode.id) ) {
   					Ext.MessageBox.show({
									title:'提示',
									msg:'是否将 ' + dropEvent.dropNode.text + ' 移动到 ' + dropEvent.target.parentNode.text + ' 下面？',
									buttons:Ext.MessageBox.OKCANCEL,
									fn:function(btn) {
										if (btn == "ok"){
											Ext.Ajax.request({
												url:"modifyOrg.action",
												params:{org_id:dropEvent.dropNode.id,operation:2,parentorg_id:dropEvent.target.parentNode.id},
												method:"post",
												callback : function(options, success, response ){
													if(success){
														dropEvent.target.parentNode.appendChild(dropEvent.dropNode);
													}
													else{
														Ext.Msg.Alert('警告','移动 '+ dropEvent.dropNode.text +" 失败！");
													}
												}		
											});
										}
									}
								});	
   				}
   				return false;
   			});
   			
   			// 定义树右键点击事件
   			tree.on('contextmenu', function(node, event) {// 声明菜单类型
   				
   						// 定义菜单添加项
				    var addOrgBtn = new Ext.menu.Item({
			      	id : 'addOrgBtn',
							text : '新增下属部门'
			      });
	      
			      // 定义菜单修改项	
			      var editOrgBtn = new Ext.menu.Item({
			      	id : 'editOrgBtn',
							text : '修改部门信息'
			      });
	      
			      // 定义菜单删除项
			      var delOrgBtn = new Ext.menu.Item({
			      	id : 'delOrgBtn',
							text : '删除选中部门',
							handler : function() {
									Ext.Msg.alert('提示','删除选中部门');
							}
			      });
	      	
			      	
				  	// 定义右键菜单
						var rightClick = new Ext.menu.Menu({
							id : 'rightClickCont',
							items:[addOrgBtn,editOrgBtn,delOrgBtn]
						});
   				
   			 	event.preventDefault();// 阻止浏览器默认右键菜单显示
   			 	
   			 	currentClickOrgId = node.id;
   				currentClickOrgName = node.text;
   			 	if (node.parentNode == null){
   			 		delOrgBtn.disable();
   			 		delOrgAction.disable();
   				}else{
   					delOrgBtn.enable();
   					delOrgAction.enable();
   					}
   				
   				node.select();
   				
   				addOrgBtn.setHandler(function() {	
   					win.setTitle('新增下属部门');
   					//win.findByType('form')[0].getForm().reset();
						win.findById('org_id').setValue(null);
						win.findById('org_code').setValue('');
						win.findById('org_name').setValue('');
						win.findById('org_memo').setValue('');
						win.findById('parent_name').setText('上级组织机构名称: ' + node.text); 
						win.findById('parentorg_id').setValue(node.id);
						win.findById('operation').setValue(1); 
						win.show();
					});
					
					editOrgBtn.setHandler(function() {
						processMask.msg = "数据加载中，请稍候...";
						processMask.show();
						Ext.Ajax.request({
									url:"getOrgInfo.action",
									params: { id: node.id},
									method:"post",
									success: function(response,options){
											processMask.hide();
											win.setTitle('修改部门信息');
											//win.findByType('form')[0].getForm().reset();
											
											var responseTextValue = Ext.util.JSON.decode(response.responseText);
											
											win.findById('org_code').setValue(Ext.util.JSON.decode(responseTextValue.jsonString)[0].org_code);
											
											if(Ext.util.JSON.decode(responseTextValue.jsonString)[0].org_memo != "null"){
												win.findById('org_memo').setValue(Ext.util.JSON.decode(responseTextValue.jsonString)[0].org_memo);
											}else{
												win.findById('org_memo').setValue('');
											}
											
											if (node.parentNode != null){
												win.findById('parent_name').setText('上级组织机构名称: ' + node.parentNode.text); 
												win.findById('parentorg_id').setValue(node.parentNode.id);
											}else{
												win.findById('parent_name').setText('');
												win.findById('parentorg_id').setValue(null);
												}
											win.findById('operation').setValue(2);
											win.findById('org_id').setValue(node.id);
											win.findById('org_name').setValue(node.text);
											win.show();
										},
									failure : function(response,options){
											processMask.hide();
										  Ext.Msg.alert('提示','获取部门原始信息失败！');
										}		
						});	
						
					});
					
					delOrgBtn.setHandler(function() {
						Ext.MessageBox.show({
							title:'提示',
							msg:'确定要删除 ' + node.text + ' 吗?',
							buttons:Ext.MessageBox.OKCANCEL,
							fn:function(btn) {
								if (btn == "ok"){
									processMask.msg = "处理中，请稍候...";
									processMask.show();
									Ext.Ajax.request({
									url:"modifyOrg.action",
									params:{org_id:node.id,operation:3},
									method:"post",
									success: function(response,options){
										processMask.hide();
										var responseTextValue = Ext.util.JSON.decode(response.responseText);
										switch(Ext.util.JSON.decode(responseTextValue.jsonString).errcode){
											case -1:
												Ext.Msg.alert("警告","当前部门拥有有效的下级部门，无法删除！");
												break;
											case -2:
												Ext.Msg.alert("警告","当前部门拥有有效的雇员，无法删除！");
												break;
											default:
												node.remove();
												currentClickOrgId = null;
												currentClickOrgName = null;
												break;
											}
											
										}	,
									failure:function(response,options){
											processMask.hide();
											var responseTextValue = Ext.util.JSON.decode(response.responseText);
											Ext.Msg.alert("警告","错误编号: "+Ext.util.JSON.decode(responseTextValue.jsonString).errcode);
										}
								});
								}
								if (btn == "cancel"){
									return;
								}
							}
						})
						 
					});
					
   			 	rightClick.showAt(event.getXY());// 取得鼠标点击坐标，展示菜单
   			 	});
   			 	
   			// 定义左键单击事件
   			tree.on('click', function(node, event) {
   				currentClickOrgId = node.id;
   				currentClickOrgName = node.text;
   				if(node.parentNode == null){
   					delOrgAction.disable();
   					}
   					else{
   						delOrgAction.enable();
   						}
   			 	});
   			
		    tree.render(Ext.getBody());
		    tree.getRootNode().expand();

			});